# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=yaml-cpp
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.7.0
pkgrel=2
pkgdesc="A YAML parser and emitter in C++ matching the YAML 1.2 spec (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://github.com/jbeder/yaml-cpp"
license=("MIT")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "${MINGW_PACKAGE_PREFIX}-pkg-config")
options=('staticlibs' 'strip')
source=(${_realname}-${pkgver}.tar.gz::https://github.com/jbeder/yaml-cpp/archive/${_realname}-${pkgver}.tar.gz
        https://patch-diff.githubusercontent.com/raw/jbeder/yaml-cpp/pull/1037.patch
        https://patch-diff.githubusercontent.com/raw/jbeder/yaml-cpp/pull/1056.patch)
sha256sums=('43e6a9fcb146ad871515f0d0873947e5d497a1c9c60c58cb102a97b47208b7c3'
            'ddc6769f46355d00515954bb2ece02c590c8f9c618ed418ca865d7c1eb9b76b1'
            '6fff9ce5b26be77b43e2f1dafbb7450e7f37e08b23c7fcb687145d202877b119')

prepare() {
  cd ${srcdir}/${_realname}-${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/1037.patch
  patch -p1 -i ${srcdir}/1056.patch
}

build() {
  msg2 "Build shared library"
  [[ -d "build-${MSYSTEM}-shared" ]] && rm -rf "build-${MSYSTEM}-shared"
  mkdir -p ${srcdir}/build-${MSYSTEM}-shared && cd ${srcdir}/build-${MSYSTEM}-shared

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_POSITION_INDEPENDENT_CODE=ON \
    -DYAML_BUILD_SHARED_LIBS=ON \
    -DYAML_CPP_BUILD_TESTS=OFF \
    ../${_realname}-${_realname}-${pkgver}
  ${MINGW_PREFIX}/bin/cmake --build .

  msg2 "Build static library"
  [[ -d "build-${MSYSTEM}-static" ]] && rm -rf "build-${MSYSTEM}-static"
  mkdir -p ${srcdir}/build-${MSYSTEM}-static && cd ${srcdir}/build-${MSYSTEM}-static

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DYAML_BUILD_SHARED_LIBS=OFF \
    -DYAML_CPP_BUILD_TESTS=OFF \
    ../${_realname}-${_realname}-${pkgver}
  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}-static"
  DESTDIR=${pkgdir} ${MINGW_PREFIX}/bin/cmake --install .

  cd "${srcdir}/build-${MSYSTEM}-shared"
  DESTDIR=${pkgdir} ${MINGW_PREFIX}/bin/cmake --install .

  install -Dm644 ${srcdir}/${_realname}-${_realname}-${pkgver}/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
